﻿Imports System.IO
Imports SecretaryToolData
Imports iTextSharp.text
Imports iTextSharp.text.pdf

Partial Public Class Reports
	'http://www.mikesdotnetting.com/Article/86/iTextSharp-Introducing-Tables

	Public Shared Function GeneratePublishersReport(Publishers As List(Of VM_PublisherInformation)) As FileInfo
		Dim fileName As String = "Publishers Report"
		If Publishers IsNot Nothing AndAlso Publishers.Count = 1 Then
			fileName &= String.Format(" - {0}", Publishers(0).FullName)
		End If
		Dim OutputFile As New FileInfo(String.Format("{0}\{1}.{2}", ConfigPropertyKeys.AutoBackupLocation, fileName.AppendFileDateExtension, "pdf"))

		Using myFileStream As New FileStream(OutputFile.FullName, FileMode.Create, FileAccess.Write)
			Using myDoc As New Document
				Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, myFileStream)

				myDoc.Open()

				Dim content As PdfContentByte = writer.DirectContent

				For Each publisher In Publishers.OrderBy(Function(x) x.FullName)

					OutputPublisherStats(myDoc, publisher)
					'add a separator line
					myDoc.Add(New Paragraph(" "))
				Next

			End Using
		End Using

		Return OutputFile

	End Function

	Public Shared Function GenerateGroupReport(GroupID As Long) As FileInfo
		Dim workingGroup As New VM_Group(GroupID)

		Dim fileName As String = "Group Report"
		If workingGroup IsNot Nothing Then
			fileName &= String.Format(" - {0}", workingGroup.GroupName)
		End If
		Dim OutputFile As New FileInfo(String.Format("{0}\{1}-{2}.{3}", ConfigPropertyKeys.AutoBackupLocation, "Group Report", workingGroup.GroupName.AppendFileDateExtension, "pdf"))

		Using myFileStream As New FileStream(OutputFile.FullName, FileMode.Create, FileAccess.Write)
			Using myDoc As New Document
				Dim writer As PdfWriter = PdfWriter.GetInstance(myDoc, myFileStream)

				myDoc.Open()

				Dim content As PdfContentByte = writer.DirectContent

				'write report header
				myDoc.Add(New Paragraph(String.Format("{0}", workingGroup.GroupName), fontPageTitle))
				myDoc.Add(New Paragraph(String.Format("Overseer: {0}", workingGroup.OverseerName), fontSectionLabel))
				myDoc.Add(New Paragraph(String.Format("Assistant: {0}", workingGroup.AssistantName), fontSectionLabel))
				'add a little spacing before table
				myDoc.Add(New Paragraph(" "))

				For Each publisher In workingGroup.GroupMembers.OrderBy(Function(x) x.FullName)

					OutputPublisherStats(myDoc, publisher)
					'add a separator line
					myDoc.Add(New Paragraph(" "))
				Next

			End Using
		End Using

		Return OutputFile

	End Function

	Public Shared Sub OutputPublisherStats(ByRef myDoc As Document, myPublisher As VM_PublisherInformation)
		Dim publisherDatum As New VM_PublisherCard(myPublisher.PublisherID)

		Dim workingRecords = publisherDatum.AllPublisherRecords.Where(Function(x) VM_Stats.lastTwelveMonths.Contains(x.ReportMonth)).OrderBy(Function(x) x.ReportMonth)

		Dim table As New PdfPTable(8)
		table.KeepTogether = True
		table.WidthPercentage = 100

		'write publisher name
		Dim titleCell As New PdfPCell(New Phrase(myPublisher.FullName, fontItem))
		titleCell.Colspan = 8
		table.AddCell(titleCell)

		'write table of records
		table.AddCell(" ")
		table.AddCell(PrettyCell("Bks."))
		table.AddCell(PrettyCell("Broch."))
		table.AddCell(PrettyCell("Hrs."))
		table.AddCell(PrettyCell("Mag."))
		table.AddCell(PrettyCell("RVs"))
		table.AddCell(PrettyCell("Studies"))
		table.AddCell(PrettyCell("Remarks"))

		For Each record In workingRecords
			table.AddCell(NoWrapCell(record.ReportMonth.ToString("MMM, yy")))
			table.AddCell(PrettyCell(record.Books))
			table.AddCell(PrettyCell(record.Brochures))
			table.AddCell(PrettyCell(record.Hours))
			table.AddCell(PrettyCell(record.Magazines))
			table.AddCell(PrettyCell(record.ReturnVisits))
			table.AddCell(PrettyCell(record.BibleStudies))
			table.AddCell(record.Remarks)
		Next

		'write 3, 6, and 12 month sums and averages
		Dim workingThreeMonthsRecords = workingRecords.Where(Function(x) VM_Stats.lastThreeMonths.Contains(x.ReportMonth))
		table.AddCell("3 Mon. Avg.")
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Average(Function(x) x.Books)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Average(Function(x) x.Brochures)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Average(Function(x) x.Hours)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Average(Function(x) x.Magazines)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Average(Function(x) x.ReturnVisits)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Average(Function(x) x.BibleStudies)))
		table.AddCell("") 'no comments for averages

		table.AddCell("6 Mon. Avg.")
		Dim workingSixMonthsRecords = workingRecords.Where(Function(x) VM_Stats.lastSixMonths.Contains(x.ReportMonth))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Average(Function(x) x.Books)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Average(Function(x) x.Brochures)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Average(Function(x) x.Hours)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Average(Function(x) x.Magazines)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Average(Function(x) x.ReturnVisits)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Average(Function(x) x.BibleStudies)))
		table.AddCell("") 'no comments for averages

		table.AddCell("12 Mon. Avg.")
		Dim workingTwelveMonthsRecords = workingRecords.Where(Function(x) VM_Stats.lastTwelveMonths.Contains(x.ReportMonth))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Average(Function(x) x.Books)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Average(Function(x) x.Brochures)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Average(Function(x) x.Hours)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Average(Function(x) x.Magazines)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Average(Function(x) x.ReturnVisits)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Average(Function(x) x.BibleStudies)))
		table.AddCell("") 'no comments for averages


		'write 3, 6, and 12 month sums and medians
		table.AddCell("3 Mon. Med.")
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Median(Function(x) x.Books)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Median(Function(x) x.Brochures)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Median(Function(x) x.Hours)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Median(Function(x) x.Magazines)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Median(Function(x) x.ReturnVisits)))
		table.AddCell(PrettyCell(workingThreeMonthsRecords.Median(Function(x) x.BibleStudies)))
		table.AddCell("") 'no comments for medians

		table.AddCell("6 Mon. Med.")
		table.AddCell(PrettyCell(workingSixMonthsRecords.Median(Function(x) x.Books)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Median(Function(x) x.Brochures)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Median(Function(x) x.Hours)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Median(Function(x) x.Magazines)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Median(Function(x) x.ReturnVisits)))
		table.AddCell(PrettyCell(workingSixMonthsRecords.Median(Function(x) x.BibleStudies)))
		table.AddCell("") 'no comments for medians

		table.AddCell("12 Mon. Med.")
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Median(Function(x) x.Books)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Median(Function(x) x.Brochures)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Median(Function(x) x.Hours)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Median(Function(x) x.Magazines)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Median(Function(x) x.ReturnVisits)))
		table.AddCell(PrettyCell(workingTwelveMonthsRecords.Median(Function(x) x.BibleStudies)))
		table.AddCell("") 'no comments for medians

		If myPublisher.IsPioneer Then
			'write publisher pioneer status
			Dim pioneerPaceHours As Integer = New VM_PublisherCard(myPublisher.PublisherID).PioneerHoursPaceThisServiceYear
			Dim footerCell As New PdfPCell(New Phrase(String.Format("This Pioneer is {0} hour{1} {2} for the service year.", Math.Abs(pioneerPaceHours), If(Math.Abs(pioneerPaceHours) > 1, "s", ""), If(pioneerPaceHours < 0, "behind", "ahead"))))
			footerCell.Colspan = 8
			table.AddCell(footerCell)
		End If

		myDoc.Add(table)
	End Sub
End Class
